package online.inote.modules.article.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import online.inote.common.core.BaseController;
import online.inote.common.core.Page;
import online.inote.common.utils.StringUtils;
import online.inote.modules.article.entity.ArticleEntity;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * <p>
 * Desc: 
 * </p>
 *
 * @author XQF SUI
 * @created 2018年8月24日 下午10:15:39
 * @version 1.0
 */
@RestController
@RequestMapping(value = "article")
@Api(tags = {"文章接口"})
public class ArticleController extends BaseController<ArticleEntity> {

	/**
	  * <p>
	  * Desc: 分页查询
	  * </p>
	  *
	  * @author XQF SUI
	  * @created 2018年8月24日 下午10:26:11
	  * @param page
	  * @param article
	  * @return
	 */
	@ApiOperation(value = "文章列表", notes = "分页查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "Integer", paramType="path"),
			@ApiImplicitParam(name = "rows", value = "每页条数", required = true, dataType = "Integer", paramType="path"),
			@ApiImplicitParam(name = "title", value = "标题", dataType = "String", paramType="path"),
			@ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "String", paramType="path"),
			@ApiImplicitParam(name = "summary", value = "简介信息", dataType = "String", paramType="path")
	})
	@RequestMapping(value = "getPage", method = RequestMethod.POST)
	public Page<ArticleEntity> getPage(Page<ArticleEntity> page, ArticleEntity article) {
		
		Example example = new Example(ArticleEntity.class);
		Criteria criteria = example.createCriteria();
		
		if (StringUtils.isNotBlank(article.getTitle())) {
			criteria.andEqualTo("title", article.getTitle());
		}
		
		if (article.getStatus() != null) {
			criteria.andEqualTo("status", article.getStatus());
		}
		
		if (StringUtils.isNotBlank(article.getSummary())) {
			criteria.andLike("summary", "%" + article.getSummary() + "%");
		}
		
		if (article.getCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo("createTime", article.getCreateTime());
		}
		
		service.selectPageByExample(page, example);
		
		return page;
	}
}
